home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / GETSM.ASM < prev    next >
Assembly Source File  |  1991-10-12  |  2KB  |  121 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7. ;
  8.         extrn    sl_malloc:far, sl_realloc:far, sl_free:far
  9.         extrn    sl_putc:far, sl_getc:far
  10. ;
  11. ;
  12. ; GETSM-Reads a line of text from the user and returns a pointer to the
  13. ;    string read.  Returns the pointer in ES:DI.  Carry=0 if no error,
  14. ;    1 if heap overflow, EOF, or some other error (DOS).
  15. ;
  16. ;    The returned string is zero terminated and does not include the
  17. ;    carriage return (ENTER) key code.
  18. ;
  19. ; Note: This routine always allocates 256 bytes when you call
  20. ;    it.
  21. ;
  22. ; Released to the public domain.
  23. ; Created by: Randall Hyde
  24. ; Date: 7/90
  25. ; Updates:
  26. ;
  27. ;    8/11/90-    Modification to handle eof and other errors.
  28. ;    10/5/91-    Renamed to new naming conventions.
  29. ;
  30. ;
  31. ;
  32. ;
  33.         public    sl_getsm
  34. sl_getsm    proc    far
  35.         push    ax
  36.         push    bx
  37.         push    cx
  38.         pushf
  39. ;
  40. ; Allocate storage for return string:
  41. ;
  42.         mov    cx, 256
  43.         call    sl_malloc
  44.         jc    BadGETS
  45. ;
  46. ; Read data from keyboard until the user hits the enter key.
  47. ;
  48.         xor    bx, bx
  49. RdKbdLp:    call    sl_getc
  50.         jc    BadGetc
  51.         cmp    ah, 0
  52.         jz    EndString
  53.         cmp    al, 0                ;Scan code?
  54.         jnz    GotKey                ;If so, ignore it.
  55.         call    sl_getc
  56.         jmp    RdKbdLp
  57. ;
  58. GotKey:        cmp    al, 08                ;Backspace
  59.         jne    NotBS
  60.         or    bx, bx                 ;Don't do it if at
  61.         jz    RdKbdLp                ; beginning of line.
  62.         dec    bx
  63.         call    sl_putc
  64.         jmp    RdKbdLp
  65. ;
  66. NotBS:        cmp    al, 13                ;See if ENTER.
  67.         jnz    NotCR
  68.         call    sl_putc
  69.         mov    al, 0ah
  70.         call    sl_putc
  71.         mov    byte ptr es:[bx][di], 0
  72.         inc    bx
  73.         jmp    GetsDone
  74. ;
  75. NotCR:        cmp    al, 1bh                ;ESC
  76.         jne    NotESC
  77.         mov    al, 8
  78. EraseLn:    call    sl_putc
  79.         dec    bx
  80.         jne    EraseLn
  81.         jmp    RdKbdLp
  82. ;
  83. NotESC:        mov    es:[bx][di], al
  84.         call    sl_putc
  85.         inc    bx
  86.         cmp    bx, 255
  87.         jb    RdKbdLp
  88.         mov    al, 7                ;Bell
  89.         call    sl_putc
  90.         dec    bx
  91.         jmp    RdKbdLp
  92. ;
  93. ; Deallocate any left over storage:
  94. ;
  95. GetsDone:    mov    cx, bx
  96.         call    sl_realloc
  97.         popf
  98.         clc
  99.         pop     cx
  100.         pop    bx
  101.         pop    ax
  102.         ret
  103. ;
  104. EndString:    mov    ax, 0            ;End of file.
  105.         call    sl_free            ;Deallocate storage
  106.         jmp    short BadGetsx
  107. ;
  108. BadGetc:    call    sl_free
  109.         jmp    short BadGetsx
  110. ;
  111. BadGets:    mov    ax, 1            ;Memory allocation error.
  112. BadGetsx:    popf
  113.         pop    cx
  114.         pop    bx
  115.         add    sp, 2            ;Don't restore AX.
  116.         stc                ;Pass error status.
  117.         ret
  118. sl_getsm    endp
  119. stdlib        ends
  120.         end
  121.